// pages/afflatus/common/customMenuItem/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    dataList:{
      type:Array,
      value:[],
      observer: function (newVal = [], oldVal = []){
        if(newVal.length > 0){
          this.handleData(newVal, this.data.selectIndex)
        }
      }
    },
    //父组件 id
    superId:{
      type:String,
      value:""
    },
    // 是否是多选
    multi:{
      type:Boolean | String,
      value:false
    },
    selectIndex:{
      type:Number | [Number],
      value:-1 | [],
      observer: function (newVal , oldVal) {
        if(newVal != null){
          this.handleData(this.data.dataList, newVal)
        }
      }

    },
    //第几个菜单
    subIndex:{
      type:Number,
      value:-1
    }
  },


  /**
   * 组件的初始数据
   */
  data: {
    menuItems:[]
  },
  created:function(){
    
  },

  attached: function () {
   
  },
  /**
   * 组件的方法列表
   */
  methods: {
 
    handleData(dataList = [], selectIndex){
      if(this.data.multi === true){ // 多选
        if (selectIndex && selectIndex.length > 0){
             selectIndex.forEach(value => {
                const [subItem] = this.data.dataList.filter(item=>{
                   return item.value === value
                 })
               subItem.selected = true
            })
            this.setData({
              dataList: this.data.dataList
            })
          }

      }else{ // 单选
       
        if (selectIndex !=null){
           const [subItem] = this.data.dataList.filter(item => {
             return item.value === selectIndex
           })
           if(subItem){
             subItem.selected = true
           }
           this.setData({
             dataList: this.data.dataList
           })
         }
      }
    },

    reset(){
      this.data.dataList.forEach((item)=>{
         item.selected = false
      })
      this.setData(
        {
          dataList: this.data.dataList
        }
      )
      const details = {
        optType:0,
        data:[],
        subIndex:this.data.subIndex,
        superId:'#' + this.data.superId
      }
      this.triggerEvent('change', details,{})
    },

    confirm(){
      const result = this.data.dataList.filter(item =>{
         return item.selected === true
      })
      const details = {
        optType:1,
        data : result,
        subIndex: this.data.subIndex,
        superId:"#" + this.data.superId
      }
      this.triggerEvent('change', details, {})
    },
    
    itemClcik(e){
      const index = e.currentTarget.dataset.index
      const selectItem = this.data.dataList[index]
      if(this.data.multi === true){
        if (selectItem.text === '全部'){
          this.data.dataList = this.data.dataList.map(item => {
            const newItem = item
            newItem.selected = false
            return newItem
          })
          selectItem.selected = !selectItem.selected
        }else{
          const [allItem] = this.data.dataList.filter(item => item.text === '全部')
          allItem.selected = false;
          selectItem.selected = !selectItem.selected
        }
        this.setData({
          dataList: this.data.dataList
        })
      }else{
        this.data.dataList = this.data.dataList.map(item=>{
            const newItem = item 
            newItem.selected = false
          return newItem
        })
        this.data.dataList[index].selected = !this.data.dataList[index].selected
        this.setData({
          dataList: this.data.dataList
        })
        const details = {
          optType: 2,
          data: this.data.dataList[index],
          subIndex: this.data.subIndex,
          superId:"#" + this.data.superId
        }
        this.triggerEvent('change', details, {})
      }
    }
  }
})
